package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;

import java.util.Map;

public class CardWhiteListAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        //模版还原时，不做校验
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode"))) {
            return;
        }
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        //变更业务的场景初始化过程中不校验
        if (ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return;
        }
        // 其中一个为空值不需要判断
        if (KiteStringUtils.isEmpty(inst.getAttrValue("200004021")) || KiteStringUtils.isEmpty(value)) {
            return;
        }

        if (!"100006427".equals(value) && !"100006427".equals(inst.getAttrValue("200004021"))) {
            LocalErrorConsts.UPLOAD_FILE_ERR_604.throwOut();
        }
    }
}
